{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exploratory Analysis of Spatial Data: Spatial Autocorrelation #\n",
    "\n",
    "The previous notebook provided several illustrations of the power of\n",
    "visualization in the analysis of spatial data. This power stems from\n",
    "visualizations ability to tap into our human pattern recognition machinery.\n",
    "\n",
    "In this notebook we introduce methods of _exploratory spatial data analysis_\n",
    "that are intended to complement geovizualization through formal univariate and\n",
    "multivariate statistical tests for spatial clustering.\n",
    "\n",
    "\n",
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import geopandas as gpd\n",
    "import libpysal.api as lp\n",
    "import matplotlib.pyplot as plt\n",
    "import rasterio as rio\n",
    "import numpy as np\n",
    "import contextily as ctx\n",
    "import shapely.geometry as geom\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = gpd.read_file('data/neighborhoods.shp')\n",
    "# was created in previous notebook with df.to_file('data/neighborhoods.shp')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have an `nan` to first deal with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.isnull(df['median_pri']).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df\n",
    "df['median_pri'].fillna((df['median_pri'].mean()), inplace=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.plot(column='median_pri')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(12,10), subplot_kw={'aspect':'equal'})\n",
    "df.plot(column='median_pri', scheme='Quantiles', k=5, cmap='GnBu', legend=True, ax=ax)\n",
    "#ax.set_xlim(150000, 160000)\n",
    "#ax.set_ylim(208000, 215000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Spatial Autocorrelation ##\n",
    "\n",
    "Visual inspection of the map pattern for the prices allows us to search for\n",
    "spatial structure. If the spatial distribution of the prices was random, then we\n",
    "should not see any clustering of similar values on the map. However, our visual\n",
    "system is drawn to the darker clusters in the south west as well as the center,\n",
    "and a concentration of the lighter hues (lower prices) in the north central and\n",
    "south east.\n",
    "\n",
    "Our brains are very powerful pattern recognition machines. However, sometimes\n",
    "they can be too powerful and lead us to detect false positives, or patterns\n",
    "where there are no statistical patterns. This is a particular concern when\n",
    "dealing with visualization of irregular polygons of differning sizes and shapes.\n",
    "\n",
    "The concept of *spatial\n",
    "autocorrelation* relates to the combination of two types of similarity: spatial\n",
    "similarity and attribute similarity. Although there are many different measures\n",
    "of spatial autocorrelation, they all combine these two types of simmilarity into\n",
    "a summary measure.\n",
    "\n",
    "Let's use PySAL to generate these two types of similarity\n",
    "measures.\n",
    "\n",
    "### Spatial Similarity ###\n",
    "\n",
    "We have already encountered spatial weights\n",
    "in a previous notebook. In spatial autocorrelation analysis, the spatial weights\n",
    "are used to formalize the notion of spatial similarity. As we have seen there\n",
    "are many ways to define spatial weights, here we will use queen contiguity:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wq =  lp.Queen.from_dataframe(df)\n",
    "wq.transform = 'r'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Attribute Similarity ###\n",
    "\n",
    "So the spatial weight between neighborhoods $i$ and $j$ indicates if the two \n",
    "are neighbors (i.e., geographically similar). What we also need is a measure of\n",
    "attribute similarity to pair up with this concept of spatial similarity. The\n",
    "**spatial lag** is a derived variable that accomplishes this for us. For neighborhood\n",
    "$i$ the spatial lag is defined as: $$y_i = \\sum_j w_{i,j} y_j$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = df['median_pri']\n",
    "ylag = lp.lag_spatial(wq, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ylag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import mapclassify.api as mc\n",
    "ylagq5 = mc.Quantiles(ylag, k=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f, ax = plt.subplots(1, figsize=(9, 9))\n",
    "df.assign(cl=ylagq5.yb).plot(column='cl', categorical=True, \\\n",
    "        k=5, cmap='GnBu', linewidth=0.1, ax=ax, \\\n",
    "        edgecolor='white', legend=True)\n",
    "ax.set_axis_off()\n",
    "plt.title(\"Spatial Lag Median Price (Quintiles)\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The quintile map for the spatial lag tends to enhance the impression of value\n",
    "similarity in space. It is, in effect, a local smoother."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['lag_median_pri'] = ylag\n",
    "f,ax = plt.subplots(1,2,figsize=(2.16*4,4))\n",
    "df.plot(column='median_pri', ax=ax[0], edgecolor='k',\n",
    "        scheme=\"quantiles\",  k=5, cmap='GnBu')\n",
    "ax[0].axis(df.total_bounds[np.asarray([0,2,1,3])])\n",
    "ax[0].set_title(\"Price\")\n",
    "df.plot(column='lag_median_pri', ax=ax[1], edgecolor='k',\n",
    "        scheme='quantiles', cmap='GnBu', k=5)\n",
    "ax[1].axis(df.total_bounds[np.asarray([0,2,1,3])])\n",
    "ax[1].set_title(\"Spatial Lag Price\")\n",
    "ax[0].axis('off')\n",
    "ax[1].axis('off')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, we still have\n",
    "the challenge of visually associating the value of the prices in a neighborhod\n",
    "with the value of the spatial lag of values for the focal unit. The latter is a\n",
    "weighted average of homicide rates in the focal county's neighborhood.\n",
    "\n",
    "To complement the geovisualization of these associations we can turn to formal\n",
    "statistical measures of spatial autocorrelation.\n",
    "\n",
    "\n",
    "## Global Spatial Autocorrelation\n",
    "\n",
    "We begin with a simple case where the variable under consideration is binary.\n",
    "This is useful to unpack the logic of spatial autocorrelation tests. So  even though\n",
    "our attribute is a continuously valued one, we will convert it to a binary case\n",
    "to illustrate the key concepts:\n",
    "\n",
    "### Binary Case"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y.median()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "yb = y > y.median()\n",
    "sum(yb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have 68 neighborhoods with list prices above the median and 70 below the\n",
    "median (recall the issue with ties)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "yb = y > y.median()\n",
    "labels = [\"0 Low\", \"1 High\"]\n",
    "yb = [labels[i] for i in 1*yb] \n",
    "df['yb'] = yb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The spatial distribution of the binary variable immediately raises questions\n",
    "about the juxtaposition of the \"black\" and \"white\" areas."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(12,10), subplot_kw={'aspect':'equal'})\n",
    "df.plot(column='yb', cmap='binary', edgecolor='grey', legend=True, ax=ax)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Join counts ###\n",
    "\n",
    "One way to formalize a test for spatial autocorrelation in a binary attribute is\n",
    "to consider the so-called _joins_. A join exists for each neighbor pair of\n",
    "observations, and the joins are reflected in our binary spatial weights object\n",
    "`wq`. \n",
    "\n",
    "Each unit can take on one of two values \"Black\" or \"White\", and so for a given\n",
    "pair of neighboring locations there are three different types of joins that can\n",
    "arise:\n",
    "\n",
    "- Black Black (BB)\n",
    "- White White (WW)\n",
    "- Black White (or White Black) (BW)\n",
    "\n",
    "Given that we have 68 Black polygons on our map, what is the number of Black\n",
    "Black (BB) joins we could expect if the process were such that the Black\n",
    "polygons were randomly assigned on the map? This is the logic of join count statistics.\n",
    "\n",
    "We can use the `esda` package from PySAL to carry out join count analysis:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import esda \n",
    "yb = 1 * (y > y.median()) # convert back to binary\n",
    "wq =  lp.Queen.from_dataframe(df)\n",
    "wq.transform = 'b'\n",
    "np.random.seed(12345)\n",
    "jc = esda.join_counts.Join_Counts(yb, wq)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The resulting object stores the observed counts for the different types of joins:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "jc.bb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "jc.ww"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "jc.bw"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the three cases exhaust all possibilities:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "jc.bb + jc.ww + jc.bw"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wq.s0 / 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "which is the unique number of joins in the spatial weights object.\n",
    "\n",
    "Our object tells us we have observed 121 BB joins:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "jc.bb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The critical question for us, is whether this is a departure from what we would\n",
    "expect if the process generating the spatial distribution of the Black polygons\n",
    "were a completely random one? To answer this, PySAL uses random spatial\n",
    "permutations of the observed attribute values to generate a realization under\n",
    "the null of _complete spatial randomness_ (CSR). This is repeated a large number\n",
    "of times (999 default) to construct a reference distribution to evaluate the\n",
    "statistical significance of our observed counts.\n",
    "\n",
    "The average number of BB joins from the synthetic realizations is:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "jc.mean_bb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "which is less than our observed count. The question is whether our observed\n",
    "value is so different from the expectation that we would reject the null of CSR?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sbn\n",
    "sbn.kdeplot(jc.sim_bb, shade=True)\n",
    "plt.vlines(jc.bb, 0, 1, color='r')\n",
    "plt.vlines(jc.mean_bb, 0,1)\n",
    "plt.xlabel('BB Counts')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The density portrays the distribution of the BB counts, with the black vertical\n",
    "line indicating the mean BB count from the synthetic realizations and the red\n",
    "line the observed BB count for our prices. Clearly our observed value is\n",
    "extremely high. A pseudo p-value summarizes this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "ptyhon"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "jc.p_sim_bb\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since this is below conventional significance levels, we would reject the null\n",
    "of complete spatial randomness in favor of spatial autocorrelation in market prices.\n",
    "\n",
    "\n",
    "### Continuous Case\n",
    "\n",
    "The join count analysis is based on a binary attribute, which can cover many\n",
    "interesting empirical applications where one is interested in presence and\n",
    "absence type phenomena. In our case, we artificially created the binary variable,\n",
    "and in the process we throw away a lot of information in our originally\n",
    "continuous attribute. Turning back to the original variable, we can explore\n",
    "other tests for spatial autocorrelation for the continuous case.\n",
    "\n",
    "First, we transform our weights to be row-standardized, from the current binary state:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wq.transform = 'r'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = df['median_pri']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Moran's I is a test for global autocorrelation for a continuous attribute:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(12345)\n",
    "mi = esda.moran.Moran(y, wq)\n",
    "mi.I"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Again, our value for the statistic needs to be interpreted against a reference\n",
    "distribution under the null of CSR. PySAL uses a similar approach as we saw in\n",
    "the join count analysis: random spatial permutations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sbn\n",
    "sbn.kdeplot(mi.sim, shade=True)\n",
    "plt.vlines(mi.I, 0, 1, color='r')\n",
    "plt.vlines(mi.EI, 0,1)\n",
    "plt.xlabel(\"Moran's I\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here our observed value is again in the upper tail, although visually it does\n",
    "not look as extreme relative to the binary case. Yet, it is still statistically significant:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mi.p_sim"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Local Autocorrelation: Hot Spots, Cold Spots, and Spatial Outliers ##\n",
    "\n",
    "In addition to the Global autocorrelation statistics, PySAL has many local\n",
    "autocorrelation statistics. Let's compute a local Moran statistic for the same\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(12345)\n",
    "import esda"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wq.transform = 'r'\n",
    "lag_price = lp.lag_spatial(wq, df['median_pri'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "price = df['median_pri']\n",
    "b, a = np.polyfit(price, lag_price, 1)\n",
    "f, ax = plt.subplots(1, figsize=(9, 9))\n",
    "\n",
    "plt.plot(price, lag_price, '.', color='firebrick')\n",
    "\n",
    " # dashed vert at mean of the price\n",
    "plt.vlines(price.mean(), lag_price.min(), lag_price.max(), linestyle='--')\n",
    " # dashed horizontal at mean of lagged price \n",
    "plt.hlines(lag_price.mean(), price.min(), price.max(), linestyle='--')\n",
    "\n",
    "# red line of best fit using global I as slope\n",
    "plt.plot(price, a + b*price, 'r')\n",
    "plt.title('Moran Scatterplot')\n",
    "plt.ylabel('Spatial Lag of Price')\n",
    "plt.xlabel('Price')\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, instead of a single $I$ statistic, we have an *array* of local $I_i$\n",
    "statistics, stored in the `.Is` attribute, and p-values from the simulation are\n",
    "in `p_sim`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "li = esda.moran.Moran_Local(y, wq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "li.q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can again test for local clustering using permutations, but here we use\n",
    "conditional random permutations (different distributions for each focal location)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(li.p_sim < 0.05).sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can distinguish the specific type of local spatial association reflected in\n",
    "the four quadrants of the Moran Scatterplot above:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sig = li.p_sim < 0.05\n",
    "hotspot = sig * li.q==1\n",
    "coldspot = sig * li.q==3\n",
    "doughnut = sig * li.q==2\n",
    "diamond = sig * li.q==4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spots = ['n.sig.', 'hot spot']\n",
    "labels = [spots[i] for i in hotspot*1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df\n",
    "from matplotlib import colors\n",
    "hmap = colors.ListedColormap(['red', 'lightgrey'])\n",
    "f, ax = plt.subplots(1, figsize=(9, 9))\n",
    "df.assign(cl=labels).plot(column='cl', categorical=True, \\\n",
    "        k=2, cmap=hmap, linewidth=0.1, ax=ax, \\\n",
    "        edgecolor='white', legend=True)\n",
    "ax.set_axis_off()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spots = ['n.sig.', 'cold spot']\n",
    "labels = [spots[i] for i in coldspot*1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df\n",
    "from matplotlib import colors\n",
    "hmap = colors.ListedColormap(['blue', 'lightgrey'])\n",
    "f, ax = plt.subplots(1, figsize=(9, 9))\n",
    "df.assign(cl=labels).plot(column='cl', categorical=True, \\\n",
    "        k=2, cmap=hmap, linewidth=0.1, ax=ax, \\\n",
    "        edgecolor='white', legend=True)\n",
    "ax.set_axis_off()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spots = ['n.sig.', 'doughnut']\n",
    "labels = [spots[i] for i in doughnut*1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df\n",
    "from matplotlib import colors\n",
    "hmap = colors.ListedColormap(['lightblue', 'lightgrey'])\n",
    "f, ax = plt.subplots(1, figsize=(9, 9))\n",
    "df.assign(cl=labels).plot(column='cl', categorical=True, \\\n",
    "        k=2, cmap=hmap, linewidth=0.1, ax=ax, \\\n",
    "        edgecolor='white', legend=True)\n",
    "ax.set_axis_off()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spots = ['n.sig.', 'diamond']\n",
    "labels = [spots[i] for i in diamond*1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df\n",
    "from matplotlib import colors\n",
    "hmap = colors.ListedColormap(['pink', 'lightgrey'])\n",
    "f, ax = plt.subplots(1, figsize=(9, 9))\n",
    "df.assign(cl=labels).plot(column='cl', categorical=True, \\\n",
    "        k=2, cmap=hmap, linewidth=0.1, ax=ax, \\\n",
    "        edgecolor='white', legend=True)\n",
    "ax.set_axis_off()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sig = 1 * (li.p_sim < 0.05)\n",
    "hotspot = 1 * (sig * li.q==1)\n",
    "coldspot = 3 * (sig * li.q==3)\n",
    "doughnut = 2 * (sig * li.q==2)\n",
    "diamond = 4 * (sig * li.q==4)\n",
    "spots = hotspot + coldspot + doughnut + diamond\n",
    "spots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spot_labels = [ '0 ns', '1 hot spot', '2 doughnut', '3 cold spot', '4 diamond']\n",
    "labels = [spot_labels[i] for i in spots]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from matplotlib import colors\n",
    "hmap = colors.ListedColormap([ 'lightgrey', 'red', 'lightblue', 'blue', 'pink'])\n",
    "f, ax = plt.subplots(1, figsize=(9, 9))\n",
    "df.assign(cl=labels).plot(column='cl', categorical=True, \\\n",
    "        k=2, cmap=hmap, linewidth=0.1, ax=ax, \\\n",
    "        edgecolor='white', legend=True)\n",
    "ax.set_axis_off()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
